<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/3/4
 * Time: 13:10
 */

namespace app\index\controller;


use app\common\controller\HomeBase;
use app\common\model\BoolStatus;
use app\common\model\FavoritesItem as FavoritesItemModel;
use tbk\top\request\TbkUatmFavoritesGetRequest;
use tbk\top\request\TbkUatmFavoritesItemGetRequest;
use think\Config;
use tbk\top\TopClient;
use think\Db;
use app\common\model\Attachment as AttachmentModel;
use base\Url;


class Tbk extends HomeBase
{

    public function index()
    {
        for ($i = 0; $i < 10; $i++) {
            for ($j = 0; $j < 2; $j++) {
                $this->xp($i,$j);
            }
        }
    }

    //淘宝客
    public function xp($PageNo = 1, $gy = 0)
    {
        $config = Config::get('tbk');

//        $gy = $this->request->param('gy');

        $appkey = $config['appkey'];
        $secret = $config['secret'];

        if ($gy) {
            //高佣女装
            $adzoneId     = '78282249';
            $favorites_id = '4233285';
        } else {
            //女装
            $adzoneId     = '74970716';
            $favorites_id = '3841878';
        }

        $pageSize = 20;
//        $PageNo   = $this->request->param('page', 1, 'int');

        $c            = new TopClient;
        $c->appkey    = $appkey;
        $c->secretKey = $secret;
        $req          = new TbkUatmFavoritesItemGetRequest();
        $req->setPlatform(1);
        $req->setPageSize($pageSize);
        $req->setAdzoneId($adzoneId);
        $req->setUnid('11');
        $req->setFavoritesId($favorites_id);
        $req->setPageNo($PageNo);
        $req->setFields("num_iid,title,pict_url,small_images,click_url,reserve_price,zk_final_price,user_type,provcity,item_url,seller_id,volume,nick,shop_title,zk_final_price_wap,event_start_time,event_end_time,tk_rate,status,type");
        $resp = $c->execute($req);

//        spe($resp);

        if (isset($resp->total_results)) {
            $total_results      = $resp->total_results;
            $results            = $resp->results;
            $dataSet            = [];
            $favoritesItemModel = new FavoritesItemModel();
            $num                = 0;
            foreach ($results as $uatm_tbk_item) {
                foreach ($uatm_tbk_item as $item) {
                    $item = (array)$item;

                    $count = $favoritesItemModel->where('num_iid', '=', $item['num_iid'])->count();
                    if (!$count) {
                        $small_images = isset($item['small_images']) ? (array)$item['small_images']->string : [];
                        $data         = [
                            'num_iid'            => $item['num_iid'],
                            'title'              => $item['title'],
                            'pict_url'           => $item['pict_url'],
                            'small_images'       => serialize($small_images),
                            'reserve_price'      => $item['reserve_price'],
                            'user_type'          => $item['user_type'],
                            'provcity'           => $item['provcity'],
                            'item_url'           => $item['item_url'],
                            'click_url'          => $item['click_url'],
                            'nick'               => $item['nick'],
                            'seller_id'          => $item['seller_id'],
                            'volume'             => $item['volume'],
                            'tk_rate'            => $item['tk_rate'],
                            'zk_final_price'     => $item['zk_final_price'],
                            'zk_final_price_wap' => $item['zk_final_price_wap'],
                            'event_start_time'   => $item['event_start_time'],
                            'event_end_time'     => $item['event_end_time'],
                            'status'             => $item['status'],
                            'type'               => $item['type'],
                            'shop_title'         => $item['shop_title'],
                            'favorites_id'       => $favorites_id,
                            'created_time'       => date('Y-m-d H:i:s', time()),
                        ];
                        $favoritesItemModel->insert($data);
                        $num++;
                    }
                    if (in_array($item['num_iid'], $this->getNumIid())) {
                        $data['status']       = $item['status'];
                        $data['updated_time'] = date('Y-m-d H:i:s', time());
                        $model                = new FavoritesItemModel();
                        $ret                  = $model->save($data, ['num_iid' => $item['num_iid']]);
                        var_dump($ret);
                    }
                }
            }

//            var_dump($num);


        } else {
            var_dump('111');
            spe($resp);
        }


    }

    /**
     * 选品库
     */
    public function xpk()
    {
        $config = Config::get('tbk');

        $appkey = $config['appkey'];
        $secret = $config['secret'];

        $pageSize = 20;
        $PageNo   = $this->request->param('page', 1, 'int');

        $c            = new TopClient;
        $c->appkey    = $appkey;
        $c->secretKey = $secret;
        $req          = new TbkUatmFavoritesGetRequest();
        $req->setPageNo($PageNo);
        $req->setPageSize($pageSize);
        $req->setFields("favorites_title,favorites_id,type");
        $req->setType(-1);
        $resp = $c->execute($req);
        spe($resp);
        die();
    }

    public function getNumIid()
    {
        $model = new FavoritesItemModel();
        $ret   = $model->column('num_iid');
//        spe($ret);
        return $ret;
    }
}